﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using VietImage.Roles;
using VietImage.Helpers;

namespace VietImageSoftware
{
	/// <summary>
	/// Interaction logic for LoginForm.xaml
	/// </summary>
	public partial class LoginForm : Window
	{		
		public LoginForm()
		{
			this.InitializeComponent();
			
			Session.CurrentUser = new RegisteredUserRole();
            Session.XmlHelper = new ReadXMLHelper();
            Session.XmlHelper.ReadXML();
            txtLIUsername.Text = Session.XmlHelper.Username;
			// Insert code required on object creation below this point.
		}
		
		#region Login
        private void btnLILogin_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //Session.CurrentUser.UserInfo = User.GetUser(1);
            //Session.CurrentUser.UserInfo.Password = VietImage.Helpers.Encoder.MD5Encode("123456");
            //User.ChangeInformation(Session.CurrentUser.UserInfo, 1, 1);
			
			
            if (Session.CurrentUser.Login(txtLIUsername.Text, txtLIPassword.Password))
            {
                if (Session.CurrentUser.UserInfo.ID_Permission == 1)
                {
                    OwnerRole owner = new OwnerRole();
                    owner.UserInfo = Session.CurrentUser.UserInfo;
                    owner.ID_Language = Session.CurrentUser.ID_Language;
                    Session.CurrentUser = owner;
                    //gridLogin.Visibility = Visibility.Hidden;
                   // gridMain.Visibility = Visibility.Visible;
                    Session.XmlHelper.Username = txtLIUsername.Text;
                    Session.XmlHelper.WriteXML();
					MainWindow form = new MainWindow();
					form.Show();
					this.Close();
                }
                else if (Session.CurrentUser.UserInfo.ID_Permission == 4)
                {
                    PartnerRole partner = new PartnerRole();
                    partner.UserInfo = Session.CurrentUser.UserInfo;
                    partner.ID_Language = Session.CurrentUser.ID_Language;
                    Session.CurrentUser = partner;
                    //cCategory.IsEnabled = false;
                    //btnUINew.Visibility = Visibility.Hidden;
                    //btnUINewSub.Visibility = Visibility.Hidden;
                    //gridLogin.Visibility = Visibility.Hidden;
                    //gridMain.Visibility = Visibility.Visible;
                    //cbMainLanguage.SelectedIndex = 0;
                    //gridLogin.Visibility = Visibility.Hidden;
                    //gridMain.Visibility = Visibility.Visible;
                    //cCategory.IsEnabled = false;
                    //cCategory.ToolTip = "Just for Owner";
                    //btnUINew.IsEnabled = false;
                    //btnUINew.ToolTip = "Just for Owner";
                    Session.XmlHelper.Username = txtLIUsername.Text;
                    Session.XmlHelper.WriteXML();
					MainWindow form = new MainWindow();
					form.Show();
					this.Close();
                }
                else
                {
                    MessageForm form = new MessageForm();
                    //form.Session.CurrentUser = Session.CurrentUser;
                    form.HaveOKButton = false;
                    form.TxtTitle = "Warning";
                    form.TxtMessage = "Access denied! You don't have permission to use this application!";
                    form.TxtCancel = "Cancel";
                    form.ShowDialog();
                }
            }
            else
            {
                MessageForm form = new MessageForm();
                //form.Session.CurrentUser = Session.CurrentUser;
                form.HaveOKButton = false;
                form.TxtTitle = "Error";
                form.TxtMessage = "Wrong username or password !";
                form.TxtCancel = "Cancel";
                form.ShowDialog();
            }
        }

        private void btnLICancel_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Close();
        }

        private void btnLIRegister_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        }
        #endregion
	}
}